<?php
/*
* I filtri di questo componente vengono settati all'interno della action executeFilter del modulo
* in quanto detta action richiede un redirect, cosa non realizzabile all'interno di un component
*/
class sfOpenGraphMetaTagPluginComponents extends sfComponents
{
  
  public function executeSetOpenGraphMetaTag(sfWebRequest $request)
  {
    $this->ogValues = array_merge(sfConfig::get('app_fbOpenGraphHelper_values'), $this->ogValues);

    $this->ogCleanValues = array();
    
    $mandatoryNodes = array('og:title', 'og:type', 'og:image', 'og:url', 'og:site_name', array('fb:admins', 'fb:app_id'));

    $missingNodes = array();
    
    foreach($mandatoryNodes as $mandatoryNode)
    {
      if(is_array($mandatoryNode))
      {
        $nodeFound = false;

        foreach($mandatoryNode as $oneNode)
        {
          if(in_array($oneNode, array_keys($this->ogValues))) $nodeFound = true;
        }

        if(!$nodeFound) $missingNodes[] = 'one of ' . implode(" or ", $mandatoryNode);
        
      }
      else
      {
        if(!in_array($mandatoryNode, array_keys($this->ogValues))) $missingNodes[] = $mandatoryNode;
      }
    }
    
    //there are not all nodes in $mandatoryNodes or are missing both fb:admins and fb:app_id
    if(count($missingNodes) > 0)
    {
      $errorMessage = 'Missing mandatory fields for Open Graph meta tags: ' . implode(", ", $missingNodes);

      $this->logMessage($errorMessage, 'err');

      if(sfConfig::get('sf_debug', false))
      {
        throw new Exception($errorMessage);
      }
    }

    foreach($this->ogValues as $ogKey => $ogValue)
    {
      //$ogKey = trim($ogKey);
      //$ogKey = (substr($ogKey, 0, 3) == "og:") ? $ogKey : "og:" . $ogKey;

      $this->ogCleanValues[$ogKey] = $ogValue;

      
    }

  }
  
}

?>
